/*
 * @Author: Zhang Wei
 * @Date: 2022-04-17 18:01:49
 * @LastEditors: Zhang Wei
 * @LastEditTime: 2022-04-17 18:21:06
 * @FilePath: \02_tank\src\service\image.ts
 * @Word: There is nothing either good or bad, but thinking makes it so.
 * @Description: 图片夫妇
 */
import config from '../config';

type mapKey = keyof typeof config.images;
export const image = new Map<mapKey, HTMLImageElement>();

export const imgLoadList = Object.entries(config.images).map(([key, value]) => {
  return new Promise((resolve, reject) => {
    const img = document.createElement('img');

    img.src = value;

    // 加载图片
    img.onload = () => {
      image.set(key as mapKey, img);
      return resolve(img);
    };

    // 加载图片失败
    img.onerror = () => {
      reject(new Error(`${value} load failed`));
    };
  });
});
